//
//  FlyBaseTableViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/9/4.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "FlyBaseTableViewController.h"
#import "EJSLoginViewController.h"

@interface FlyBaseTableViewController ()
{
    UIView *_noDataView;
}

@end

@implementation FlyBaseTableViewController

+ (instancetype)viewController
{
    return nil;
}

#pragma mark Setter/Getter

- (NSMutableArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


#pragma mark  lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    if(self.navigationController.viewControllers.count > 1)
    {
        [self configNavigationLeftItemWithImage:nil];
    }
    
    //设置背景
    UIView *bgView = [UIView new];
    bgView.backgroundColor = UIColorFromRGB(0xf3f4f6);
    [self.tableView setBackgroundView:bgView];
    //设置分割线颜色
    [self.tableView setSeparatorColor:UIColorFromRGB(0xf3f4f6)];
    
    UITableViewHeaderFooterView *headerFooterView = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.0001f)];
    headerFooterView.backgroundColor = UIColorFromRGB(0xf3f4f6);
    headerFooterView.contentView.backgroundColor = UIColorFromRGB(0xf3f4f6);
    //隐藏没有数据时显示的分割线
    self.tableView.tableHeaderView = headerFooterView;
    self.tableView.tableFooterView = headerFooterView;
    
    [self configOwnViews];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.backgroundColor = kWhiteColor;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:UIColorFromRGB(0x3C4151)};
    
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillShow:)
//                                                 name:UIKeyboardWillShowNotification
//                                               object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillHide:)
//                                                 name:UIKeyboardWillHideNotification
//                                               object:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//#pragma mark 键盘出现
//-(void)keyboardWillShow:(NSNotification *)note
//{
//    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);
//}
//#pragma mark 键盘消失
//-(void)keyboardWillHide:(NSNotification *)note
//{
//    self.tableView.contentInset = UIEdgeInsetsZero;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configNavigationLeftItemWithImage:(UIImage *)image
{
    UIImage *leftImage = [UIImage imageNamed:@"back_icon"];
    if(image)
    {
        leftImage = image;
    }
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(leftBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:leftImage forState:UIControlStateNormal];
    [btn setImage:leftImage forState:UIControlStateHighlighted];
    [btn sizeToFit];;
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
}

- (void)configNavigationRightItemWithImage:(UIImage *)image
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateHighlighted];
    [btn sizeToFit];
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = leftBarItem;
}

- (void)configNavigationRightItmeWithTitle:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton sizeToFit];
    [rightButton.titleLabel setFont:font];
    
    [rightButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:titleColor forState:UIControlStateNormal];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    [self.navigationItem setRightBarButtonItem:rightBarItem];
}


- (void)configOwnViews
{
    
}

- (BOOL)userIsLogin{
    if(![EJSUserTokenModel userTokenModel]){
        EJSLoginViewController *loginVC = [[EJSLoginViewController alloc] initWithNibName:@"EJSLoginViewController" bundle:nil];
        FlyNavigationController *nav = [[FlyNavigationController alloc] initWithRootViewController:loginVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:YES completion:nil];
        return NO;
    }
    return YES;
}


- (NSInteger)pageSize
{
    return 10;
}


#pragma mark - Actions
- (void)leftBarButtonAction:(id)sender
{
    if(self.navigationController.viewControllers.count > 1)
    {
        if(self.isNeedPopToRoot){
             [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)rightBarButtonAction:(id)sender
{
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColorFromRGB(0xe5e8ec);
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColorFromRGB(0xe5e8ec);
    return view;
}

-(void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    SEL selector = NSSelectorFromString(@"_alertController");
    if ([actionSheet respondsToSelector:selector])//ios8 以后采用UIAlertController来代替uiactionsheet和UIAlertView
    {
        UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];
        if ([alertController isKindOfClass:[UIAlertController class]])
        {
            alertController.view.tintColor = [UIColor blackColor];
        }
    }
    else//ios7 之前采用这样的方式
    {
        for( UIView * subView in actionSheet.subviews )
        {
            if( [subView isKindOfClass:[UIButton class]] )
            {
                UIButton * btn = (UIButton*)subView;
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                
            }
        }
    }
}

- (void)showNoDataViewWithText:(NSString *)text
{
    [self showNoDataViewWithText:text offset:0];
}

- (void)showNoDataViewWithText:(NSString *)text offset:(CGFloat)offset
{
    [self hideNoDataView];
    
    _noDataView = [[UIView alloc] initWithFrame:self.view.bounds];
    _noDataView.backgroundColor = kClearColor;
    [self.view addSubview:_noDataView];
    
    NSString *noDataMsg = text?text:NSLocalizedString(@"Esta vacio", nil);
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.textColor = kBlackColor;
    textLabel.font = kSystemFontSize(15);
    textLabel.text = noDataMsg;
    [_noDataView addSubview:textLabel];
    
    [textLabel sizeToFit];
    textLabel.center = _noDataView.center;
    textLabel.top += offset;
    if (textLabel.bottom + 20 > self.view.height)
    {
        textLabel.bottom = self.view.height - 20;
    }
}

- (void)hideNoDataView
{
    if(_noDataView)
    {
        [_noDataView removeFromSuperview];
        _noDataView = nil;
    }
}


@end


@implementation FlyBaseTableViewController (Refresh)


/**
 添加下拉刷新
 */
- (void)addHeaderRefreshAction
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshAction)];
}

/**
 添加上拉加载
 */
- (void)addFooterRefreshAction
{
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshAction)];
    footer.automaticallyHidden = YES;
    self.tableView.mj_footer = footer;
}

/**
 addHeaderRefreshAction | addFooterRefreshAction
 */
- (void)addRefreshAction
{
    [self addHeaderRefreshAction];
    [self addFooterRefreshAction];
}

/**
 下拉刷新
 */
- (void)headerRefreshAction
{
    
}
/**
 上拉加载
 */
- (void)footerRefreshAction
{
    
}

/**
 结束刷新
 */
- (void)endRefresh
{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

@end

